/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import fse from 'fs-extra';
import { resolve } from 'path';

import { BuildDirConst } from '../../src/const/build-directory-const';

import { initByStageEmptyApp, initByStageEmptyService } from './common/common';
import { ProjectBuildProfileModifier } from './common/modifier/project-build-profile-modifier';
import { Probes } from './common/probes';
import { generateDefaultStageBuildConfig, testPreview } from './common/task/common-preview';

describe('test Preview Stage app', () => {
  test.concurrent('stage app entry page preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      const moduleName = 'entry';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      testPreview(projectDir, moduleName, 'page', buildConfig);
      const probes = new Probes(projectDir, resolve(projectDir, 'entry'), 'default', 'default', '.preview');
      expect(fse.existsSync(resolve(probes.getIntermediatesRes(), BuildDirConst.IDS_MAP))).toBeFalsy();
    },);
  });

  test.concurrent('stage app entry card preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      const moduleName = 'entry';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      buildConfig.stageRouterConfig.contents[1] = '{"src":[]}';
      testPreview(projectDir, moduleName, 'card', buildConfig);
    },);
  });

  test.concurrent('stage app entry api10 preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      const moduleName = 'entry';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      buildConfig.stageRouterConfig.contents[1] = '{"src":[]}';
      testPreview(projectDir, moduleName, 'page', buildConfig);
      const probes = new Probes(projectDir, resolve(projectDir, 'entry'), 'default', 'default', '.preview');
      console.log(fse.readdirSync(resolve(probes.getIntermediatesRes(), BuildDirConst.IDS_MAP)));
      expect(fse.readdirSync(resolve(probes.getIntermediatesRes(), BuildDirConst.IDS_MAP)).length).toBeGreaterThan(0);
    }, {
      prepare: async (dir) => {
        const projectProfileJsonModifier = new ProjectBuildProfileModifier(dir);
        projectProfileJsonModifier.setRuntimeInfo('HarmonyOS', '4.0.0(10)', '4.0.0(10)').outputSync();
      }
    });
  });

  test.concurrent('stage app feature preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      const moduleName = 'feature';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      testPreview(projectDir, moduleName, 'page', buildConfig);
    });
  });

  test.concurrent('stage app library preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      const moduleName = 'library';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      buildConfig.stageRouterConfig.contents[1] = '{"src":["components/mainpage/MainPage"]}';
      testPreview(projectDir, moduleName, 'page', buildConfig,
        ['previewer.replace.page=components/mainpage/MainPage']);
    });
  });

  test.concurrent('stage app vlibrary preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      const moduleName = 'vlibrary';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      buildConfig.stageRouterConfig.contents[1] = '{"src":["components/mainpage/MainPage"]}';
      testPreview(projectDir, moduleName, 'page', buildConfig,
        ['previewer.replace.page=components/MainPage/MainPage']);
    });
  });

  test.concurrent('stage app slibrary preview', async () => {
    await initByStageEmptyApp(async (projectDir) => {
      const moduleName = 'slibrary';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      testPreview(projectDir, moduleName, 'page', buildConfig);
    });
  });
});

describe('test Preview Stage service', () => {
  test.concurrent('stage service entry page preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      const moduleName = 'entry';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      testPreview(projectDir, moduleName, 'page', buildConfig);
    },);
  });

  test.concurrent('stage service entry card preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      const moduleName = 'entry';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      buildConfig.stageRouterConfig.contents[1] = '{"src":[]}';
      testPreview(projectDir, moduleName, 'card', buildConfig);
    },);
  });

  test.concurrent('stage service feature preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      const moduleName = 'feature';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      testPreview(projectDir, moduleName, 'page', buildConfig);
    });
  });

  test.concurrent('stage service library preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      const moduleName = 'library';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      buildConfig.stageRouterConfig.contents[1] = '{"src":["components/mainpage/MainPage"]}';
      testPreview(projectDir, moduleName, 'page', buildConfig,
        ['previewer.replace.page=components/mainpage/MainPage']);
    });
  });

  test.concurrent('stage service vlibrary preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      const moduleName = 'vlibrary';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      buildConfig.stageRouterConfig.contents[1] = '{"src":["components/mainpage/MainPage"]}';
      testPreview(projectDir, moduleName, 'page', buildConfig,
        ['previewer.replace.page=components/MainPage/MainPage']);
    });
  });

  test.concurrent('stage service slibrary preview', async () => {
    await initByStageEmptyService(async (projectDir) => {
      const moduleName = 'slibrary';
      const buildConfig = generateDefaultStageBuildConfig(projectDir, moduleName);
      testPreview(projectDir, moduleName, 'page', buildConfig);
    });
  });
});
